package webhook

import (
	"errors"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
	"strings"
	"time"
)

const (
	TimeOut    = 10
	RetryCycle = 3
)

var Wecom = new(wecom)

type wecom struct {
}

// 企业微信机器人推送
func (w *wecom) Send(url string, content map[string]interface{}) (string, error) {
	client := g.Client().ContentJson().SetTimeout(TimeOut * time.Second)
	//设置发送多个
	urlArr := strings.Split(url, "|")
	for _, v := range urlArr {
		result := client.SetRetry(RetryCycle, TimeOut*time.Second).PostContent(v, content)
		response := gconv.Map(result)
		if gconv.Int(response["errcode"]) != 0 {
			return "", errors.New("推送失败,错误码:" + gconv.String(response["errcode"]))
		}
	}

	return "", nil
}
